//获取应用实例
var app = getApp();

var MyRequest = require("../../utils/MyRequest.js");
var Global = require("../../Global.js");
var util = require('../../utils/util.js');

Page({
  data: {
      PageCount: 6,
      Page: 1,
  },
  Presenter: {
      init: function(that) {
          var data = {
              action: "list",
              userId: Global.getUserStorage().id,
              begin: 0,
              end: that.data.Page * that.data.PageCount
            };
          MyRequest.MyGetWithToast("OrderList", that, that.Binder.bindOrderList, data);
      },
      getDeliveredDate: function(that) {
          var data = {"action":"weekday"}
          MyRequest.MyGet("ActiveDay",that,that.Binder.bindDeliverDate,data)
      },
      Refresh: function(that) {
          that.setData({
              Page: 1
          });
        that.Presenter.init(that);
      },
      Pull: function (that) {
        that.setData({
            Page: that.data.Page + 1
        });
        that.Presenter.init(that);
    }
  },
  Binder: {
      bindOrderList: function(result, that) {
          var data = result.data.data;
          var dataList = new Array();
          for(var i = 0; i < data.length; i ++) {
              var curOrder = data[i];
              var curOrderList = curOrder.shopData;
              for(var item in curOrderList) {
                  var curProduct = curOrderList[item];
                  var sendDate = new Date(curProduct[0].sendDate);
                  var week = sendDate.getDay();
                  var weekday=new Array(7);
                  weekday[0]="周日";
                  weekday[1]="周一";
                  weekday[2]="周二";
                  weekday[3]="周三";
                  weekday[4]="周四";
                  weekday[5]="周五";
                  weekday[6]="周六";
                  curProduct[0].week = weekday[week];
                  curOrder.oneProduct = curProduct[0];
                  break;
              }
              dataList.push(curOrder);
          }
          that.setData({
              OrderList: dataList
          });
      },
      bindDeliverDate: function(result,that) {
          var dateList = new Array();
          var data = result.data.data;
          that.setData({
            DeliveredDate: data
        });
      }
  },
  onReady: function() {
     
  },
  onLoad: function () {
      this.Presenter.getDeliveredDate(this);
      this.Presenter.init(this);
  },
  onShow: function() {
    //   this.Presenter.init(this);
  },
  onHide: function() {
  },
  onUnload: function() {
  },
  onPullDownRefresh: function() {
      this.Presenter.Refresh(this);
  },
  onReachBottom: function() {
      this.Presenter.Pull(this);
  },
  toOrderInfo:function(event){
      var index = event.currentTarget.dataset.index;
      var params = "?id="+this.data.OrderList[index].id;
      wx.navigateTo({
        url: '/pages/orderInfo/orderInfo'+params
      })
  },
  toComment:function(event){
      var index = event.currentTarget.dataset.index;
      var params = "?orderNo="+this.data.OrderList[index].orderNo;
        params += "&orderTime=";
        params += this.data.OrderList[index].orderTime;
        params += "&id=";
        params += this.data.OrderList[index].id;
        wx.navigateTo({
            url: '/pages/comment/comment'+params
        })
  }
})